package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ProjContractLogic;
import cn.topevery.garden.entity.dto.ProjContractDTO;
import cn.topevery.garden.entity.dto.ProjContractFilesDTO;
import cn.topevery.garden.entity.po.ProjContract;
import cn.topevery.garden.entity.query.ProjContractQuery;
import cn.topevery.garden.entity.vo.ProjContractVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目合同表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Api(tags = "工程项目-项目合同表")
@RestController
@RequestMapping("/projContract")
public class ProjContractController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    ProjContractLogic projContractLogic;

    /**
     * 保存项目合同表信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增项目合同表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody ProjContractFilesDTO dto) {
        projContractLogic.saveOrUpdate(getUser().userId, dto);
        return JsonResult.ok(true);
    }

    /**
     * 新增项目合同表信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增项目合同表信息")
    @PostMapping("/insert")
    public JsonResult<Boolean> insert(@Valid @RequestBody ProjContractDTO dto) {
        projContractLogic.save(getUser().userId, dto);
        return JsonResult.ok(true);
    }

    /**
     * 获取项目合同表列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目合同表列表")
    @PostMapping("/list")
    public JsonResult<List<ProjContractVO>> list(@RequestBody ProjContractQuery query) {
        return JsonResult.ok(projContractLogic.list(query));
    }

    /**
     * 获取项目合同表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目合同表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ProjContractVO>> listPage(ProjContractQuery query) {
        return JsonResult.ok(projContractLogic.listPage(query));
    }

    /**
     * 根据自增ID批量删除
     *
     * @param dto
     * @return
     */
    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody ProjContractDTO dto) {
        projContractLogic.delete(getUser().userId, dto.getIds());
        return JsonResult.ok(true);
    }

    /**
     * 编辑项目合同表信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("编辑项目合同表信息")
    @PostMapping("/update")
    public JsonResult<Boolean> update(@Valid @RequestBody ProjContractDTO dto) {
        projContractLogic.update(getUser().userId, dto);
        return JsonResult.ok(true);
    }

    /**
     * 根据自增id获取详情
     *
     * @param id
     * @return
     */
    @ApiOperation("根据自增id获取详情")
    @GetMapping("/getById")
    public JsonResult<ProjContractVO> getById(@RequestParam(value = "id") Integer id) {
        ProjContract projContract = projContractLogic.getEntity(id);
        ProjContractVO vo = mapper.map(projContract, ProjContractVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("导出项目合同表信息")
    @GetMapping("/export")
    public void export(ProjContractQuery query, HttpServletResponse response) {
        List<ProjContractVO> list = projContractLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "项目合同表", "sheet1", ProjContractVO.class, "项目合同表", response);
    }

}
